package com.spreadk.webdemo.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * todo：法二：使用注解
 */
@WebServlet(name = "Demo1", urlPatterns = {"/demo1","/demo3"})
public class DemoController extends HttpServlet {

    @Override
    public void init() throws ServletException {
        System.out.println("---------------demo servlet init ----------------");
    }

    @Override
    public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        System.out.println("---------------demo servlet service ----------------");
        // option : 用来区别后台操作,做分发
        Integer option = Integer.parseInt(req.getParameter("option"));
        switch (option) {
            case 2:
                add(req,res);
                break;
            default:
                list(req,res);
                break;
        }

    }


    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        System.out.println("接收参数name：" + name);
        System.out.println("------------------执行get请求-----------------");
        resp.getWriter().println("hello，00000000");
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("--------------------执行 add 方法 -----------------");
    }
}
